const express = require('express');
const router = express.Router();
const serviceFactory = require('../ServiceFactory/ServiceFactory.js');
const ResultJson = require('../model/ResultJson.js');
const ExcelUtils = require("../utils/ExcelUtils.js")

router.get('/list',async (req,resp) => {
        let results = await serviceFactory.recordsService.getAllList();
        // console.log('请求全部结果',results);
        
        resp.json(new ResultJson(true,"数据请求成功",results));
})

router.get('/getListByPage',async (req,resp) => {
        let results = await serviceFactory.roomInfoService.getListByPage(req.query)
        resp.json(new ResultJson(true,"数据请求成功",results))
})

router.get('/findById',async (req,resp) => {
        let {id} = req.query;
        let results = await serviceFactory.roomInfoService.findById(id);
        resp.json(new ResultJson(true,"数据请求成功",results));
})

router.post('/update',async (req,resp) => {
        let results = await serviceFactory.roomInfoService.update(req.body);
        resp.json(new ResultJson(results,results?"修改成功":"修改失败"))
})

router.get('/delete',async (req,resp) => {
        let results = await serviceFactory.roomInfoService.deleteId(req.query);
        resp.json(new ResultJson(results,results?"修改成功":"修改失败"))
})

router.get("/exportExcel",async (req,resp) => {
    let results = await serviceFactory.roomInfoService.exportExcel(req.query);
    let excelPath = ExcelUtils.resultsToExcel(results);
    if(excelPath){
        resp.sendFile(excelPath);
    }else{
        resp.status(500).json(new ResultJson(false,"excel没有数据，不能下载"))
    }  
})

module.exports = router;